/*
 * @Description: title
 * @Author: Carlton
 * @Date: 2019-09-16 14:25:05
 * @LastEditTime: 2019-12-05 09:31:45
 * @LastEditors: select last editor
 */


#include <string.h>

#include "fontlib.h"

#include "app_util.h"

int font_lib_load_header(fl_header_t * flh,  uint8_t * data, uint16_t size)
{
    flh->magic = uint32_decode(data);
    if(flh->magic != 0x4C464453){ //magic is not match, exist 
        return 1; 
    }
    flh->size = uint32_decode(data + 4);

    flh->num_of_section = data[8];
    flh->height_of_font = data[9];
    flh->code_flag      = data[10];
    
    return 0; 
}

int font_lib_load_section_inf(fl_section_inf_t * flsi, uint8_t *data, uint16_t size)
{
    flsi->first = uint16_decode( data);
    flsi->last = uint16_decode( data + 2);
    flsi->offset = uint32_decode( data + 4);

    return 0; 
}

int font_lib_find_unicode_address(uint16_t unicode, fl_section_inf_t *flsi, uint32_t * p_offset)
{
    if(  unicode < flsi->first || unicode > flsi->last)
    {
        return 1;
    }

    *p_offset = ((uint32_t)unicode - flsi->first) * sizeof(char_desc_data_t) + flsi->offset; 

    return 0;
}
